﻿<Window x:Class="Kinectomix.GestureRecorder.View.RecorderWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ViewModel="clr-namespace:Kinectomix.GestureRecorder.ViewModel"
        xmlns:Control="clr-namespace:Kinectomix.GestureRecorder.Control"
        xmlns:Converter="clr-namespace:Kinectomix.GestureRecorder.Converter"
        xmlns:System="clr-namespace:System;assembly=mscorlib"
        Icon="../ApplicationIcon.ico"
        Title="Gesture Recorder" Height="600" Width="600" MinHeight="600">
    <Window.DataContext>
        <ViewModel:RecorderViewModel />
    </Window.DataContext>
    <Window.Resources>
        <Converter:RecordingTimeoutToVisibilityConverter x:Key="RecordingTimeoutToVisibilityConverter" />
        <Converter:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
        <Converter:IsRecognizedToBrushConverter x:Key="IsRecognizedToBrushConverter"/>
    </Window.Resources>
    <Grid>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="150" MinWidth="150" />
                <ColumnDefinition Width="5"/>
                <ColumnDefinition />
                <ColumnDefinition Width="5"/>
                <ColumnDefinition Width="150" MinWidth="10" />
            </Grid.ColumnDefinitions>


            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <TextBlock Margin="2" Grid.Row="0" Text="Recording" FontSize="18" FontWeight="ExtraLight" />

                <TextBlock Margin="2,2,2,0" Grid.Row="1" Text="Gesture recording duration" FontWeight="ExtraLight" />
                <TextBox Margin="2,0,2,2" Grid.Row="2" TextAlignment="Center" Text="{Binding RecordingDuration, Mode=TwoWay}" />

                <TextBlock Margin="2,2,2,0" Grid.Row="3" Text="Gesture name" FontWeight="ExtraLight" />
                <TextBox Margin="2,0,2,2" Grid.Row="4" TextAlignment="Center" Text="{Binding GestureName, Mode=TwoWay}" />

                <TextBlock Margin="2,2,2,0" Grid.Row="5" Text="Gesture id" FontWeight="ExtraLight" />
                <TextBox Margin="2,0,2,2" Grid.Row="6" TextAlignment="Center" Text="{Binding GestureId, Mode=TwoWay}" />

                <TextBlock Margin="2" Grid.Row="7" Text="Tracked joints" FontSize="18" FontWeight="ExtraLight" />

                <Canvas Grid.Row="8" VerticalAlignment="Center" Height="380"  Width="145">

                    <!-- Krk -->
                    <Line
                X1="70" Y1="30"
                X2="70" Y2="60"
                Stroke="Black"
                StrokeThickness="4" />


                    <!--  prava ruka -->
                    <Line X1="70" X2="120" Y1="60" Y2="70" Stroke="Black" />

                    <!-- Head -->
                    <CheckBox IsChecked="{Binding TrackedJoints.Head}" Canvas.Left="62" Canvas.Top="20" />

                    <!-- Spine -->
                    <CheckBox IsChecked="{Binding TrackedJoints.Spine}" Canvas.Left="62" Canvas.Top="143" />


                    <!--  leva ruka -->
                    <Line X1="70" X2="10" Y1="60" Y2="70" Stroke="Black" />

                    <!-- leva paze -->
                    <Line X1="10" X2="8" Y1="70" Y2="120" Stroke="Black" />

                    <!-- prava paze -->
                    <Line X1="120" X2="125" Y1="70" Y2="120" Stroke="Black" />

                    <!-- prave predlokti -->
                    <Line X1="125" X2="125" Y1="120" Y2="170" Stroke="Black" />

                    <!-- leve predlokti -->
                    <Line X1="10" X2="10" Y1="120" Y2="170" Stroke="Black" />

                    <!-- leva ruka -->
                    <Line X1="125" X2="125" Y1="170" Y2="190" Stroke="Black" />

                    <!-- prava ruka -->
                    <Line X1="10" X2="10" Y1="170" Y2="190" Stroke="Black" />

                    <!-- telo -->
                    <Line
                X1="70" Y1="60"
                X2="70" Y2="140"
                Stroke="Black"
                StrokeThickness="4" />

                    <!-- bricho -->
                    <Line
                X1="70" Y1="140"
                X2="70" Y2="170"
                Stroke="Black"
                StrokeThickness="4" />

                    <!-- levy bok-->
                    <Line X1="70" X2="45" Y1="170" Y2="190" Stroke="Black"/>

                    <!-- pravy bok-->
                    <Line X1="70" X2="95" Y1="170" Y2="190" Stroke="Black"/>

                    <!-- prava noha -->
                    <Line X1="95" X2="95" Y1="190" Y2="280" Stroke="Black" />

                    <!-- leva noha -->
                    <Line X1="40" X2="40" Y1="190" Y2="280" Stroke="Black" />

                    <!-- leva holen -->
                    <Line X1="40" X2="40" Y1="280" Y2="330" Stroke="Black" />

                    <!-- prava holen -->
                    <Line X1="95" X2="95" Y1="280" Y2="330" Stroke="Black" />

                    <!-- prave chodidlo -->
                    <Line X1="95" X2="125" Y1="330" Y2="360" Stroke="Black" />

                    <!-- leve chodidlo -->
                    <Line X1="40" X2="10" Y1="330" Y2="360" Stroke="Black" />

                    <!-- Head -->
                    <CheckBox IsChecked="{Binding TrackedJoints.Head}" Canvas.Left="62" Canvas.Top="20" />

                    <!-- Spine -->
                    <CheckBox IsChecked="{Binding TrackedJoints.Spine}" Canvas.Left="62" Canvas.Top="143" />

                    <!-- Hip Center -->
                    <CheckBox IsChecked="{Binding TrackedJoints.HipCenter}" Canvas.Left="62" Canvas.Top="165" />

                    <!-- Hip Left -->
                    <CheckBox IsChecked="{Binding TrackedJoints.HipLeft}" Canvas.Left="35" Canvas.Top="180" />

                    <!-- Hip Right -->
                    <CheckBox IsChecked="{Binding TrackedJoints.HipRight}" Canvas.Left="90" Canvas.Top="180" />

                    <!-- Knee Right -->
                    <CheckBox IsChecked="{Binding TrackedJoints.KneeRight}" Canvas.Left="90" Canvas.Top="270" />

                    <!-- Knee Left -->
                    <CheckBox IsChecked="{Binding TrackedJoints.KneeLeft}" Canvas.Left="35" Canvas.Top="270" />

                    <!-- Ankle Right -->
                    <CheckBox IsChecked="{Binding TrackedJoints.AnkleRight}" Canvas.Left="90" Canvas.Top="325" />

                    <!-- Ankle Left -->
                    <CheckBox IsChecked="{Binding TrackedJoints.AnkleRight}" Canvas.Left="35" Canvas.Top="325" />

                    <!-- Foot Left -->
                    <CheckBox IsChecked="{Binding TrackedJoints.FootLeft}" Canvas.Left="5" Canvas.Top="350" />

                    <!-- Foot Right -->
                    <CheckBox IsChecked="{Binding TrackedJoints.FootRight}" Canvas.Left="115" Canvas.Top="350" />

                    <!-- Shoulder center -->
                    <CheckBox IsChecked="{Binding TrackedJoints.ShoulderCenter}" Canvas.Left="62" Canvas.Top="53" />

                    <!-- Left shoulder -->
                    <CheckBox IsChecked="{Binding TrackedJoints.ShoulderLeft}" Canvas.Left="9" Canvas.Top="63" />

                    <!-- Right shoulder -->
                    <CheckBox IsChecked="{Binding TrackedJoints.ShoulderRight}" Canvas.Left="116" Canvas.Top="63" />

                    <!-- Right elbow -->
                    <CheckBox IsChecked="{Binding TrackedJoints.ElbowRight}" Canvas.Left="120" Canvas.Top="110" />

                    <!-- Left elbow -->
                    <CheckBox IsChecked="{Binding TrackedJoints.ElbowLeft}" Canvas.Left="3" Canvas.Top="110" />

                    <!-- Left wrist -->
                    <CheckBox IsChecked="{Binding TrackedJoints.WristLeft}" Canvas.Left="3" Canvas.Top="160" />

                    <!-- Right wrist -->
                    <CheckBox IsChecked="{Binding TrackedJoints.WristRight}" Canvas.Left="120" Canvas.Top="160" />

                    <!-- Left hand -->
                    <CheckBox IsChecked="{Binding TrackedJoints.HandLeft}" Canvas.Left="3" Canvas.Top="180" />

                    <!-- Right hand -->
                    <CheckBox IsChecked="{Binding TrackedJoints.HandRight}" Canvas.Left="120" Canvas.Top="180" />
                </Canvas>
                <Button Margin="2" Grid.Row="9" Content="_Record gesture..." Command="{Binding StartRecordingCommand}" />
            </Grid>

            <GridSplitter Grid.Column="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" ResizeBehavior="PreviousAndNext" BorderBrush="Silver" BorderThickness="1,0" />

            <Grid Grid.Column="2">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition />
                </Grid.RowDefinitions>



                <Grid Grid.Row="1">
                   
                    <Viewbox HorizontalAlignment="Center" VerticalAlignment="Center" Stretch="Uniform">
                        <!-- colorViewer and skeletonViewer should overlap entirely. -->
                        <Grid>
                            <Control:KinectStreamViewer x:Name="ColorViewer" KinectSensor="{Binding Sensor}" CollectFrameRate="True" RetainImageOnSensorChange="True" />
                            <Canvas>
                                <Control:KinectSkeletonViewer 
                                    KinectSensor="{Binding Sensor}"
                                    Width="{Binding ElementName=ColorViewer,Path=ActualWidth}"
                                    Height="{Binding ElementName=ColorViewer,Path=ActualHeight}"
                                    ImageType="Color" />
                            </Canvas>
                        </Grid>
                    </Viewbox>

                    <TextBlock HorizontalAlignment="Left" 
                           VerticalAlignment="Top" 
                           FontSize="20"
                           Foreground="Orange"
                           TextAlignment="Center"
                           Visibility="{Binding IsRecording, Converter={StaticResource BooleanToVisibilityConverter}}">
                    <Run Text="Remaining time:" />
                    <Run Text="{Binding RecordingRemainingTime}" />
                    </TextBlock>

                    <TextBlock HorizontalAlignment="Right" 
                           VerticalAlignment="Top" 
                           FontSize="20"
                           Foreground="Orange"
                           TextAlignment="Center"
                           Visibility="{Binding IsRecording, Converter={StaticResource BooleanToVisibilityConverter}}">
                    <Run Text="Recorded frames:" />
                    <Run Text="{Binding RecordedFrameCount}" />
                    </TextBlock>

                    <TextBlock Text="{Binding LastRecognizedGesture.Name}" 
                               HorizontalAlignment="Center" 
                               VerticalAlignment="Center" 
                               FontSize="52" 
                               Foreground="Orange">
                        <TextBlock.Style>
                            <Style TargetType="{x:Type TextBlock}">
                                <Setter Property="Opacity" Value="0" />
                                
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding ShowRecognizedGestureName}">
                                        <DataTrigger.Value>
                                            <System:Boolean>True</System:Boolean>
                                        </DataTrigger.Value>

                                        <DataTrigger.EnterActions>
                                            <StopStoryboard BeginStoryboardName="FadeOut"/>
                                        </DataTrigger.EnterActions>
                                        
                                        <Setter Property="Opacity" Value="1" />

                                        <DataTrigger.ExitActions>
                                            <BeginStoryboard Name="FadeOut">
                                                <Storyboard>
                                                    <DoubleAnimation Storyboard.TargetProperty="(TextBlock.Opacity)"
                                                                     From="1.0"
                                                                     To="2.0"
                                                                     Duration="0:0:2"
                                                                     AutoReverse="True"
                                                                     RepeatBehavior="1x" />
                                                </Storyboard>
                                            </BeginStoryboard>
                                        </DataTrigger.ExitActions>
                                    </DataTrigger>

                                </Style.Triggers>
                            </Style>
                        </TextBlock.Style>
                    </TextBlock>

                    <TextBlock Text="{Binding DtwCost}" 
                               HorizontalAlignment="Center" 
                               VerticalAlignment="Bottom" 
                               FontSize="42" 
                               Foreground="Orange" />
                    
                    <TextBlock
                               HorizontalAlignment="Left" 
                               VerticalAlignment="Bottom" 
                               FontSize="32" 
                               Foreground="Orange">
                        <Run Text="FPS:" />
                        <Run Text="{Binding Fps}" />
                    </TextBlock>
                </Grid>
            </Grid>

            <GridSplitter Grid.Column="3" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" ResizeBehavior="PreviousAndNext" BorderBrush="Silver" BorderThickness="1,0" />

            <Grid  Grid.Column="4">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <StackPanel>
                    <TextBlock Margin="2" Grid.Row="0" Text="Recognizing" FontSize="18" FontWeight="ExtraLight" />

                    <Button Margin="2" Content="Start recognizing" Command="{Binding StartRecognizingCommand}" />

                    <TextBlock Margin="2,2,2,0" Grid.Row="1" Text="Available gestures" FontWeight="ExtraLight" />
                </StackPanel>

                <ItemsControl ItemsSource="{Binding Gestures}" Margin="2,0,2,2" Grid.Row="1">
                    <ItemsControl.Template>
                        <ControlTemplate>
                            <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
                                <ItemsPresenter />
                            </ScrollViewer>
                        </ControlTemplate>
                    </ItemsControl.Template>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Border Background="{Binding IsRecognized, Converter={StaticResource IsRecognizedToBrushConverter}}" 
                                    Margin="2" 
                                    BorderThickness="1" 
                                    BorderBrush="Azure"
                                    Tag="{Binding DataContext,RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Window}}}">
                                <Border.ContextMenu>
                                    <ContextMenu>
                                        <MenuItem Header="Delete" 
                                                  Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ContextMenu}, Path=PlacementTarget.Tag.RemoveGestureCommand}" 
                                                  CommandParameter="{Binding}" />
                                    </ContextMenu>
                                </Border.ContextMenu>
                            <StackPanel>
                                    <TextBlock FontWeight="Bold">
                                        <Run Text="{Binding Name}" />
                                    </TextBlock>
                                    <TextBlock>
                                        <Run Text="Id: " />
                                        <Run Text="{Binding Id}" />
                                    </TextBlock>
                                    <TextBlock>
                                        <Run Text="Frames: " />
                                        <Run Text="{Binding Sequence.Count, Mode=OneWay}" />
                                    </TextBlock>
                            </StackPanel>
                            </Border>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>

                <Button Grid.Row="2" Margin="2" Content="_Add gesture" Command="{Binding AddGestureCommand}" />
            </Grid>

        </Grid>
        <Grid Background="White" Opacity="0.7" Visibility="{Binding RecordingTimeout, Converter={StaticResource RecordingTimeoutToVisibilityConverter}}">
            <StackPanel VerticalAlignment="Center">
                <TextBlock FontSize="32" TextAlignment="Center" VerticalAlignment="Center" HorizontalAlignment="Center">
                <Run Text="Prepare for gesture recording..." />
                <LineBreak />
                </TextBlock>
                <TextBlock FontSize="32" 
                           TextAlignment="Center" 
                           Foreground="Red"
                           Text="Waiting for skeleton..." 
                           Visibility="{Binding IsActiveSkeleton, Converter={StaticResource BooleanToVisibilityConverter}, ConverterParameter='1'}" />
                <TextBlock FontSize="32"
                           Foreground="Green"
                           TextAlignment="Center" 
                           Visibility="{Binding IsActiveSkeleton, Converter={StaticResource BooleanToVisibilityConverter}}"
                           Text="{Binding RecordingTimeout.TotalSeconds}" />
            </StackPanel>
        </Grid>
    </Grid>
</Window>
